/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.FhAllDevice;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.FhAllDeviceRepository;
import me.zhengjie.service.FhAllDeviceService;
import me.zhengjie.service.dto.FhAllDeviceDto;
import me.zhengjie.service.dto.FhAllDeviceQueryCriteria;
import me.zhengjie.service.mapstruct.FhAllDeviceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wuhao
* @date 2020-09-28
**/
@Service
@RequiredArgsConstructor
public class FhAllDeviceServiceImpl implements FhAllDeviceService {

    private final FhAllDeviceRepository fhAllDeviceRepository;
    private final FhAllDeviceMapper fhAllDeviceMapper;

    @Override
    public Map<String,Object> queryAll(FhAllDeviceQueryCriteria criteria, Pageable pageable){
        Page<FhAllDevice> page = fhAllDeviceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fhAllDeviceMapper::toDto));
    }

    @Override
    public List<FhAllDeviceDto> queryAll(FhAllDeviceQueryCriteria criteria){
        return fhAllDeviceMapper.toDto(fhAllDeviceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FhAllDeviceDto findById(Integer id) {
        FhAllDevice fhAllDevice = fhAllDeviceRepository.findById(id).orElseGet(FhAllDevice::new);
        ValidationUtil.isNull(fhAllDevice.getId(),"FhAllDevice","id",id);
        return fhAllDeviceMapper.toDto(fhAllDevice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FhAllDeviceDto create(FhAllDevice resources) {
        return fhAllDeviceMapper.toDto(fhAllDeviceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FhAllDevice resources) {
        FhAllDevice fhAllDevice = fhAllDeviceRepository.findById(resources.getId()).orElseGet(FhAllDevice::new);
        ValidationUtil.isNull( fhAllDevice.getId(),"FhAllDevice","id",resources.getId());
        fhAllDevice.copy(resources);
        fhAllDeviceRepository.save(fhAllDevice);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            fhAllDeviceRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FhAllDeviceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FhAllDeviceDto fhAllDevice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("ip地址", fhAllDevice.getIp());
            map.put("端口", fhAllDevice.getPort());
            map.put("用户名", fhAllDevice.getUserName());
            map.put("密码", fhAllDevice.getPassWord());
            map.put("设备名称", fhAllDevice.getName());
            map.put("设备编号", fhAllDevice.getCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}